package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.BrandAddNewDTO;
import cn.tedu.csmall.product.service.IBrandService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@Api(tags = "03. 品牌管理模块")
@RestController
@RequestMapping("/brands")
public class BrandController {
    @Autowired
    IBrandService brandService;

    //http://localhost:8080/brand/add-new?name=TestAlbumName001&description=TestDescription001&sort=77
    @PostMapping("/add-new")
    @ApiOperation("添加品牌")//将添加品牌替换成addNew
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Validated BrandAddNewDTO brandAddNewDTO){

        /*try {
            brandService.addNew(brandAddNewDTO);
            return "品牌添加成功！";
        }catch (RuntimeException e){
            return "品牌添加失败，尝试添加的品牌名称已被占用！";
        }*/
        log.debug("开始处理[属性模板]的请求,参数: {}", brandAddNewDTO);
        brandService.addNew(brandAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:8080/brands/delete?id=1
    @ApiOperation("删除品牌")
    @ApiOperationSupport(order = 200)
    @PostMapping("/delete")
    public JsonResult delete(Long id){
        log.debug("开始处理[删除品牌数据]的请求,参数:{}",id);
        brandService.delete(id);
        return JsonResult.ok();
    }
}
